﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TSupplierRepository : BaseRepository<TSupplier>, ITSupplierRepository
    {
        public TSupplierRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sup"></param>
        /// <returns></returns>
        public bool AddTSupplier(TSupplier sup)
        {
            try
            {
                var supplist = Context.TSuppliers.AsNoTracking().AsQueryable();
                if (supplist.Any())
                {
                    supplist = supplist.OrderByDescending(x => x.Code);
                    int code = Convert.ToInt32(supplist.First().Code);
                    code++;
                    sup.Code = code.ToString("D3");
                }
                else
                    sup.Code = "001";
                //供应商类型
                var dicitemobj = Context.TDictItems.Find(sup.TypeId);
                if(dicitemobj!=null)
                {
                    sup.TypeName = dicitemobj.ItemName;
                }
                sup.CreateUserId = sup.NowUserId;
                sup.CreateUserName = sup.NowUserRealName;
                sup.CreateDate = DateTime.Now;

                Context.TSuppliers.Add(sup);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelTSupplier(int id)
        {
            try
            {
                var sup = Context.TSuppliers.Find(id);
                if (sup is not null)
                {
                    Context.TSuppliers.Remove(sup);
                    return Context.SaveChanges() > 0;
                }
                return false;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<TSupplier> GetAllTSupplier(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var supList = Context.TSuppliers.AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    supList = supList.Where(c => c.Name.Contains(name));
                }
                totalCount = supList.Count();
                supList = supList.OrderBy(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return supList.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public TSupplier GetSupplierById(int id)
        {
            try
            {
                return Context.TSuppliers.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<TDictItem> GetTSupplierDic()
        {
            try
            {
                int dicTypeId=0;
                var dicType = Context.TDictTypes.Where(c=>c.TypeName=="供应商类型");
                var resList=new List<TDictItem>();
                if (dicType.Count()!=0)
                {
                    dicTypeId = dicType.First().Id;
                    foreach (var item in Context.TDictItems)
                    {
                        if (item.TypeId == dicTypeId)
                            resList.Add(item);
                    }
                }
                return resList;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public bool UpdTSupplier(TSupplier supplier)
        {
            try
            {
               
                //供应商类型
                var dicitemobj = Context.TDictItems.Find(supplier.TypeId);
                if (dicitemobj != null)
                {
                    supplier.TypeName = dicitemobj.ItemName;
                }
                supplier.ModifyDate = DateTime.Now;
                supplier.ModifyUserId = supplier.NowUserId;
                supplier.ModifyUserName = supplier.NowUserRealName;
                Context.TSuppliers.Update(supplier);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询所有供应商
        /// </summary>
        /// <returns></returns>
        public List<TSupplierDto> GetAllTSupplier(int pageIndex, int pageSize, out int totalCount)
        {
            List<TSupplierDto> supplierDtos = new List<TSupplierDto>();
            var list = Context.TSuppliers.AsQueryable();
            foreach (var item in list)
            {
                TSupplierDto supplierDto = new TSupplierDto()
                {
                    Code = item.Code,
                    Name = item.Name,
                    Contact = item.Contact,
                    ContactMobile = item.ContactMobile,
                };
                supplierDtos.Add(supplierDto);
            }
            totalCount = supplierDtos.Count();
            return supplierDtos.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

        }
    }
}
